﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Media;
using SkinSharp;
using System.Runtime.InteropServices;
namespace MMDemo
{
    public partial class SysMainForm : Form
    {
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        public static SysDataStructsForm AnimationForm;
        public static SkinH_Net skinh;
        public SysMainForm()
        {
            skinh = new SkinH_Net();
           // skinh.Attach();
            InitializeComponent();
            SetStyle(
                ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
        }

        
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show(this,"确定退出？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }

        

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                PublicDataClass.languageflag = 1;//C++语言
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                PublicDataClass.languageflag = 2;//C语言
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                PublicDataClass.languageflag = 3;//java语言
            }
        }

       
        private void Init(int type)
        {
            PublicDataClass.functionflag = type;
            switch (PublicDataClass.functionflag)
            {
                case 11: GlobalList.Operate_Type = ListOperateType.Insert;
                    break;
                case 12: GlobalList.Operate_Type = ListOperateType.Delete;
                    break;
                case 13: GlobalList.Operate_Type = ListOperateType.Combination;
                    break;
                case 14: GlobalList.Operate_Type = ListOperateType.Search;
                    break;
                case 21: GlobalLink.operateType = LinkOperateType.InitNew;//初始化当前操作类型为链表的新建
                    break;

                case 22: GlobalLink.operateType = LinkOperateType.AddElement;
                    break;
                case 23: GlobalLink.operateType = LinkOperateType.Delete;
                    break;
                case 24: GlobalLink.operateType = LinkOperateType.Search;
                    break;
                case 31: GlobalQueue.operateType = QueueOperateType.YangHui;
                    break;
                case 32: GlobalQueue.operateType = QueueOperateType.CirQueue;
                    break;
                case 33: GlobalQueue.operateType = QueueOperateType.CirQueueAuto;
                    break;
                case 41: GlobalStack.operateType = StackOperateType.Queen;
                    break;
                case 42: GlobalStack.operateType = StackOperateType.Hanoi;
                    break;
                case 43: GlobalStack.operateType = StackOperateType.SeqStack;
                    break;
                case 44: GlobalStack.operateType = StackOperateType.SeqStackAuto;
                    break;
                case 51: GlobalBinTree.operateType = OperateTypeBinTree.CreTree;
                    break;
                case 52: GlobalBinTree.operateType = OperateTypeBinTree.PreOrder;
                    break;
                case 53: GlobalBinTree.operateType = OperateTypeBinTree.InOrder;
                    break;
                case 54: GlobalBinTree.operateType = OperateTypeBinTree.PostOrder;
                    break;
                case 55: GlobalBinTree.operateType = OperateTypeBinTree.Hoffman;
                    break;
                case 61: GlobalGraphs.OperatType = GraphsOperateType.BFT;
                    break;
                case 62: GlobalGraphs.OperatType = GraphsOperateType.DFT;
                    break;
                case 63: GlobalGraphs.OperatType = GraphsOperateType.Kruskal;
                    break;
                case 64: GlobalGraphs.OperatType = GraphsOperateType.Prim;
                    break;
                case 65: GlobalGraphs.OperatType = GraphsOperateType.TopologicalSortCheckLoop;
                    break;
                case 66: GlobalGraphs.OperatType = GraphsOperateType.DFTCheckLoop;
                    break;
                case 67: GlobalGraphs.OperatType = GraphsOperateType.Dijkstra;
                    break;
                case 68: GlobalGraphs.OperatType = GraphsOperateType.Bellman_Ford;
                    break;
                case 71: GlobalSort.operateType = OperateTypeSort.Select;
                    break;
                case 72: GlobalSort.operateType = OperateTypeSort.Insert;
                    break;
                case 73: GlobalSort.operateType = OperateTypeSort.Bubble;
                    break;
                case 74: GlobalSort.operateType = OperateTypeSort.Shell;
                    break;
                case 75: GlobalSort.operateType = OperateTypeSort.Quick;
                    break;
                case 81: GlobalSearch.operateType = OperateTypeSearch.Seqsch;
                    break;
                case 82: GlobalSearch.operateType = OperateTypeSearch.Halfsch;
                    break;
                case 83: GlobalSearch.operateType = OperateTypeSearch.Insertsch;
                    break;
                case 84: GlobalSearch.operateType = OperateTypeSearch.Fibonacci;
                    break;
                case 91: GlobalTree.operateType = OperateTypeTree.Directory;
                    break;
                case 92: GlobalTree.operateType = OperateTypeTree.Parent;
                    break;
                case 93: GlobalTree.operateType = OperateTypeTree.Recessed;
                    break;
            }
            //new一个演示主窗体
            AnimationForm = new SysDataStructsForm();
            
            this.Hide();//隐藏主界面
            AnimationForm.ShowDialog();//显示数据演示框
            this.Show();//当数据演示框关闭的时候，显示主界面
        }

       
        private void timer1_Tick(object sender, EventArgs e)
        {
            IntPtr hWnd = FindWindow("#32770", "SkinSE");
            if (hWnd != IntPtr.Zero)
            {
                SendMessage(hWnd, 0x0010, 0, 0);
            }
            LabelEx templa = new LabelEx();
            for (int i = 0; i < list_sd.Count; ++i)
            {
                if (!list_sd[i].stopflag)
                {
                    templa = list_la[i];
                    list_sd[i].y = (list_sd[i].y + 3) % 600;
                    ChangeLableTrans(ref templa);
                    list_la[i].Location = new Point(list_sd[i].x, list_sd[i].y);
                }

            }

        }
        String path = Application.StartupPath;
        public static PlayMedia bgmplay = new PlayMedia();


        public Bitmap[] BgPictures = new Bitmap[6];
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            Animation_height = this.Height;
            bgmplay.SetMusicPath(PublicDataClass.Imagepath + "/music/宫崎骏 - 龙猫.mp3");
            this.checkBox1.Checked = PublicDataClass.IsOpenBackMusic;
            checkBox1_CheckedChanged(sender,e);
            List_Init();
            InitLable();


            string[] name = { "background1.jpg", "background2.jpg", "background3.jpg", "background4.jpg", "background5.jpg", "background6.jpg" };
            for (int i = 0; i < 6; ++i)
            {
                BgPictures[i] = (Bitmap)Image.FromFile(path + "/Resources/" + name[i]);
            }
        }

        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            // MessageBox.Show(m.Msg.ToString()); 
            if (m.Msg == 0x00A3)
            {
                m.WParam = System.IntPtr.Zero;
            }
            base.WndProc(ref m);
        }

        List<LabelEx> list_la = new List<LabelEx>();


        private LabelEx la;
        private void InitLable()
        {
            for (int i = 0; i < list_sd.Count; ++i)
            {
                la = new LabelEx();
                la.MouseClick += new MouseEventHandler(this.Label_MouseClick);
                la.MouseEnter += new EventHandler(this.Label_MouseEnter);
                la.MouseLeave += new EventHandler(this.Label_MouseLeave);
                la.Font = new Font("方正舒体", 15);
                la.ForeColor = Color.FromArgb(50, Color.Red);
                la.AutoSize = true;
                la.Name = "lable1" + i;
                la.BackColor = Color.Transparent;
                la.Text = list_sd[i].name;
                la.Location = new Point(list_sd[i].x, list_sd[i].y);
                this.Controls.Add(la);
                list_la.Add(la);
            }

        }

        private int FindIndex(object sender, EventArgs e)
        {
            Label templab = (Label)sender;
            int num = 0;
            for (int i = 0; i < list_sd.Count; ++i)
            {
                if (templab.Text == list_sd[i].name)
                {
                    num = i;
                    break;
                }
            }
            return num;
        }
        private void Label_MouseClick(object sender, EventArgs e)
        {
            int n = FindIndex(sender, e);
            Init(list_sd[n].functionflag);

        }

        private void Label_MouseEnter(object sender, EventArgs e)
        {
            int i = FindIndex(sender, e);

            list_sd[i].stopflag = true;
            this.Cursor = Cursors.Hand;
        }

        private void Label_MouseLeave(object sender, EventArgs e)
        {
            int i = FindIndex(sender, e);
            list_sd[i].stopflag = false;

            this.Cursor = Cursors.Arrow;
        }
        public int Animation_height = 0;

        public void ChangeLableTrans(ref LabelEx la)
        {
            int transkey = 255;
            if (la.Location.Y <= Animation_height / 2)
            {
                transkey = (int)((la.Location.Y * 1.0 / Animation_height) * 510);
            }
            else
            {
                transkey = Animation_height / 2 - la.Location.Y % (Animation_height / 2);
                transkey = (int)((transkey * 1.0 / Animation_height) * 510);
            }
            la.Font = new Font(bgFont, (float)(transkey / 15.0) + 5);
            la.alpha = transkey;
            la.brs = bgColor;
        }

        public class StringData
        {
            public string name;
            public int x;
            public int y;
            public int functionflag;
            public int transparency;
            public bool stopflag;
            public StringData(string str, int sdx, int sdy, int fg)
            {
                name = str;
                x = sdx;
                y = sdy;
                transparency = 50;
                stopflag = false;
                functionflag = fg;
            }
        }
        public List<StringData> list_sd = new List<StringData>();
        StringData tempsd;
        private void List_Init()
        {
            string codeurl = "code/function.txt";
            StreamReader objReader = new StreamReader(codeurl);
            string sLine = "";
            int x = 0;
            int y = 0;
            int fg = 0;
            while (sLine != null)
            {
                sLine = objReader.ReadLine();
                x = Convert.ToInt32(objReader.ReadLine());
                y = Convert.ToInt32(objReader.ReadLine());
                fg = Convert.ToInt32(objReader.ReadLine());
                if (sLine != null && !sLine.Equals(""))
                {
                    tempsd = new StringData(sLine, x, y, fg);
                    list_sd.Add(tempsd);
                }
            }
            objReader.Close();
        }


        
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            PublicDataClass.IsOpenBackMusic = checkBox1.Checked;
            ChangBackMusicState(PublicDataClass.IsOpenBackMusic);
        }
        public static void ChangBackMusicState(bool state)
            {                
                if (state)
                {
                    bgmplay.PlaySong();
                }
                else
                {
                    bgmplay.StopSong();
                }
            }
        private string CurMusicName = "";
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            CurMusicName = comboBox1.Text;
            try
            {
                bgmplay.SetMusicPath(PublicDataClass.Imagepath + "/music/" + CurMusicName);
                ChangBackMusicState(PublicDataClass.IsOpenBackMusic);
            }
            catch
            {
                MessageBox.Show(this,"该音乐文件不存在!");
                bgmplay.SetMusicPath(PublicDataClass.Imagepath + "/music/宫崎骏 - 龙猫.mp3");
                ChangBackMusicState(PublicDataClass.IsOpenBackMusic);
            }
        }

        public Image bgImage = null;
        public Color bgColor = Color.YellowGreen;
        public string bgFont = "方正舒体";

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ThemeName = comboBox2.Text;
            switch (ThemeName)
            {
                case "主题1":
                    {
                        bgImage = BgPictures[0];
                        bgColor = Color.YellowGreen;
                        bgFont = "方正舒体";
                    }
                    break;
                case "主题2":
                    {
                        bgImage = BgPictures[1];
                        bgColor = Color.Black;
                        bgFont = "隶书";
                    }
                    break;
                case "主题3":
                    {
                        bgImage = BgPictures[2];
                        bgColor = Color.Purple;
                        bgFont = "华文行楷";
                    }
                    break;
                case "主题4":
                    {
                        bgImage = BgPictures[3];
                        bgColor = Color.Wheat;
                        bgFont = "华文新魏";
                    }
                    break;
                case "主题5":
                    {
                        bgImage = BgPictures[4];
                        bgColor = Color.Cyan;
                        bgFont = "楷体";
                    }
                    break;
                case "主题6":
                    {
                        bgImage = BgPictures[5];
                        bgColor = Color.Cyan;
                        bgFont = "方正舒体";
                    }
                    break;
            }
            this.BackgroundImage = bgImage;
            this.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void 插入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(11);
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(12);
        }

        private void 合并ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(13);
        }

        private void 查找ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Init(14);
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(21);
        }

        private void 插入ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Init(22);
        }

        private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Init(23);
        }

        private void 查找ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Init(24);
        }

        private void 杨辉三角ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(31);
        }

        private void 皇后问题ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(41);
        }

        private void 汉诺塔ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(42);
        }

        private void 先序新建ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Init(51);
        }

        private void 先序遍历ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(52);
        }

        private void 中序遍历ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(53);
        }

        private void 后序遍历ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(54);
        }

        private void 霍夫曼树ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(55);
        }

        private void 广度优先BFTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(61);
        }

        private void 深度优先DFTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(62);
        }

        private void krusalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(63);
        }

        private void 普特姆Prim算法ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(64);
        }

        private void 拓朴排序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(65);
        }

        private void 选择排序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(71);
        }

        private void 插入排序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(72);
        }

        private void 冒泡排序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(73);
        }

        private void 希尔排序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(74);
        }

        private void 快速排序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(75);
        }

        private void 顺序查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(81);
        }

        private void 折半查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(82);
        }

        private void 插入查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(83);
        }

        private void 斐波拉契ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Init(84);
        }

        private void 目录结构ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(91);
        }
        private void 凹入表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(93);
        }
        private void 父指针ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(92);
        }

        private void 深度优先ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(66);
        }

        private void dijkstraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(67);
        }

        private void bellmanFordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(68);
        }

        private void 自动出入队ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(33);
        }

        private void 手动出入队ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(32);
        }

        private void 自动出入栈ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(44);
        }

        private void 手动出入栈ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init(43);
        }

        
       
    }   
}
